<template>

  <div class="form-box">
    <div class="form-header">基本信息</div>
    <div class="form-body">
      <el-form :model="model" :rules="rules" ref="model" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号：" prop="accountName">
          <el-input v-model="model.accountName"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="model.password"></el-input>
        </el-form-item>
        <el-form-item label="姓名：" prop="realName">
          <el-input v-model="model.realName"></el-input>
        </el-form-item>
        <el-form-item label="联系方式：" prop="linkphone">
          <el-input v-model="model.linkphone"></el-input>
        </el-form-item>
        <el-form-item label="所属岗位：" prop="groupId">
          <el-input v-model="model.groupId"></el-input>
        </el-form-item>
        <el-form-item label="图片：" prop="img">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
          >
            <img v-if="model.img" :src="model.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('model')">{{$t('action.comfirm')}}</el-button>
          <el-button @click="close">{{$t('action.cancel')}}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: "AccountSetting",
    data() {
      return {
        model: {
          accountName: "",
          password: "",
          realName: "",
          linkphone: "",
          groupId: null,
          img: "",
        },
        rules: {
          accountName: [
            {required: true, message: '请输入账号', trigger: 'blur'},
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
          ],
          realName: [
            {required: true, message: '请输入姓名', trigger: 'blur'},
          ],
          linkphone: [
            {required: true, message: '请输入联系方式', trigger: 'blur'},
          ],
          groupId: [
            {required: true, message: '请选择所属岗位', trigger: 'blur'},
          ],
          img: [
            {required: true, message: '请选择头像', trigger: 'blur'},
          ],
        },
        isShowChooseCourse: false,
      }
    },
    created() {
      // if (this.$route.query.id || this.id) this.getAccount(this.$route.query.id || this.id);
      this.getAccount(this.$route.query.id || this.id);
    },
    methods: {
      getAccount(id) {
        this.$api.account.getAccount({id: id}).then(
          res => {
            this.model = res.data;
          }
        )
      },
      close(data) {
        this.$emit("closePop", data)
      },
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('提交成功!');
          } else {
            console.log('提交失败!!');
            return false;
          }
        });
      },
    }
  }
</script>

<style lang="scss" scoped>
  .form-box {
    margin: 10px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, .05);

    .form-header {
      height: 42px;
      line-height: 42px;
      padding: 0 15px;
      border-bottom: 1px solid #f6f6f6;
      color: #333;
      border-radius: 2px 2px 0 0;
      font-size: 14px;
      text-align: left;
      font-weight: bolder;
    }

    .form-body {
      position: relative;
      padding: 10px 15px;
      line-height: 24px;

      .avatar {
        width: 200px;
      }
    }
  }

  .el-form {
    width: 40%;
    margin: 0 auto
  }

  .radio-group.is-error {
    color: red;

    .el-form-item__error {
      left: 86px;
    }
  }
</style>
